# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Bruce Zhang

pkgname=sonic
pkgver=1.4.0
pkgrel=1
pkgdesc='Fast, lightweight & schema-less search backend'
arch=('loong64' 'x86_64')
url='https://github.com/valeriansaliou/sonic'
license=('MPL2')
depends=('gcc-libs')
makedepends=('git' 'rust' 'clang' 'llvm')
backup=('etc/sonic.cfg')
options=('!lto')
_commit='f5302f5c424256648ba0be32b3c5909d846821fe'
source=(
  "$pkgname::git+$url.git#commit=$_commit"
  'systemd.service'
  'sysusers.conf'
  'tmpfiles.conf'
)
sha512sums=('SKIP'
            'f623f4076210f5387d3b3b6e61e0d45323a5d9fbc139a3a658d3f8c317cbf863976e0e9c604eb1f03ca42f2f339d733382961b2b73d33464ec8154d8a9e18475'
            '0fd64f7959024ecab487924d277679b81c78d1ba7d57d2dc63f654409f23bb0cbb0109ac513b25f1b1785115c3a9916f8c4e230fe94963f0bbe84f2784ec76e4'
            '62b0bb63ba2eda59d530b38aeaf581f5b5fc5187c4615694be9e4a7e38dcb5ed55367b461ac483ab3d6c931d1754b980c8e48b5518388b8476437d988956883b')
b2sums=('SKIP'
        'f4e605968aa9cc8a5dccf98c60bbce77502e7bcba74f7d3d7ffbe6fedd7e7ac21006837d7c99cba0bf2dfc50a58fc78744861d16aa902dadbbb367ec26d7e0be'
        '5e75e9b670baded2f3956849d2b7b0a90f5a573629f5812d8332b26d203425f14e131eabb2bb485cddf533c27a67ebdb4e87671324d316fa083d7ecdbeba6221'
        'ea9795c42003660522d89156eced726bc7db686ef8545bc57504e55341982be2e2336e9d872ce17d5c1607e0cd98e9a2f28f5836bd60b56e510ec5fd59c77143')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed "s/^v//"
}

prepare() {
  cd "$pkgname"

  # download dependencies
  cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
}

build() {
  cd "$pkgname"

  cargo build --frozen --release
}

check() {
  cd "$pkgname"

  cargo test --frozen
}

package() {
  # systemd integration
  install -vDm644 systemd.service "$pkgdir/usr/lib/systemd/system/sonic.service"
  install -vDm644 sysusers.conf "$pkgdir/usr/lib/sysusers.d/sonic.conf"
  install -vDm644 tmpfiles.conf "$pkgdir/usr/lib/tmpfiles.d/sonic.conf"

  cd "$pkgname"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" target/release/sonic


  # configuration
  install -vDm644  config.cfg "$pkgdir/etc/sonic.cfg"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" ./*.md
}
